package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.example.demo.bean.QueryInfo;
import com.example.demo.bean.User;
import com.example.demo.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

//用户管理
@RestController
public class UserController {

    @Autowired
    private UserDao userDao;

    @RequestMapping("to_user")
    public String toUser(@RequestBody User user){
        String flag = "error";
        User us = userDao.getUserMessage(user.getUsername(),user.getPassword());
        System.out.println("User : " + us);
        HashMap<String, Object> res = new HashMap<>();
        if(us != null)
            flag = "ok"; //用于表示登录操作的状态
        res.put("flag",flag);
        res.put("user",us);
        return JSON.toJSONString(res);
    }

    //获取所有用户信息
    @RequestMapping("/allUser")
    public String getUserList(QueryInfo queryInfo){
        //模糊查询
        int  numbers =  userDao.getUserCounts("%"+queryInfo.getQuery()+"%");
        //开始页面
        int pageStart = (queryInfo.getPageStart()-1)*queryInfo.getPageSize();
        List<User> users = userDao.getAllUser("%"+queryInfo.getQuery()+"%",pageStart,queryInfo.getPageSize());
        HashMap<String,Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data",users);
        String res_string = JSON.toJSONString(res);
        return res_string;
    }

    //修改用户状态
    @RequestMapping("/userState")
    public String updateUserState(@RequestParam("id") Integer  id,
                                  @RequestParam("state") Boolean state){
        int i = userDao.updateState(id, state);
        System.out.println("用户编号:"+id);
        System.out.println("用户状态:"+state);
        String str = i >0?"success":"error";
        return str;
    }

    //修改用户状态
    @RequestMapping("/userRole")
    public String updateUserRole(@RequestParam("id") Integer  id,
                                 @RequestParam("role") String role){
        int i = userDao.updateUserRole(id, role);
        System.out.println("用户编号:"+id);
        System.out.println("用户状态:"+role);
        String str = i >0?"success":"error";
        return str;
    }

    //添加用户信息
    @RequestMapping("/addUser")
    public String addUser(@RequestBody User user){
        System.out.println(user);
        user.setRole("普通管理员");
        user.setState(true);
        int i = userDao.addUser(user);
        String str = i >0?"success":"error";
        return str;
    }

    //删除用户信息
    @RequestMapping("/deleteUser")
    public String deleteUser(int id){
        System.out.println(id);
        int i = userDao.deleteUser(id);
        String str = i >0?"success":"error";
        return str;
    }

    //获取当前用户信息
    @RequestMapping("/getUpdate")
    public String getUpdateUser(int id){
        System.out.println("编号:"+id);
        User updateUser = userDao.getUpdateUser(id);
        String users_json = JSON.toJSONString(updateUser);
        return users_json;
    }

    //修改信息
    @RequestMapping("/editUser")
    public String editUser(@RequestBody User user){
        System.out.println(user);
        int i = userDao.editUser(user);
        String str = i >0?"success":"error";
        return str;
    }
}
